#include "tools.h"

//获取指令
int get_cmd(char start,char end)
{
    
	puts("******************");
	printf("请输入指令:");
	while(1)
	{
		int cmd =getch();
		if(start <= cmd && cmd <= end)
		{
			printf("%c\n",cmd);
			return cmd;
		}
	}
}



//获取字符串
char* get_str(char* str,size_t size)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
	assert(NULL!=str && size>1);
	size_t len;
	while(1)
	{
		len = strlen(fgets(str,size,stdin));
		if(1==len)
		{
			printf("\n输入不能为空，请重新输入：");
		}
		else
		{
			break;
		}
	}
	if('\n' == str[len-1])
	{
		str[len-1] = '\0';
	}
	else
	{
		while('\n' != getchar());
	}
	return str;
}

//延时显示
void show_msg(const char* msg,float sec)
{
	printf("\033[01;32m%s\033[00m\n",msg);
	usleep(sec*1000000);
}


//按任意键继续
void anykey_continue(void)
{
    stdin->_IO_read_ptr = stdin->_IO_read_end;
	puts("\n按任意键继续...");
	getch();
}

//获取密码
char* get_passwd(char* passwd,size_t size)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;

	int i = 0;
	while(i < size-1)
	{
		passwd[i] = getch();
		// 读取到退格键
		if(127 == passwd[i])
		{
			// 数组中有已输入密码
			if(i > 0)
		    {
		    	// 删除一位密码
		    	i--;
		    	printf("\b \b");
	    	}
	    	continue;
	    }
		i++;
		printf("*");
	}
	passwd[size-1] = '\0';
	printf("\n");
	return passwd;
}



//得到数量
int file_num(char *NAME,char* name)
{
	char NAME_path[255];
	sprintf(NAME_path,"%s%s",NAME,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL == rfp)
		return 0;
	int num;
	fscanf(rfp,"%d",&num);
	fclose(rfp);
	return num;
}


//消除某一记录
bool del_record(char* NAME,char* name,char* record)
{
	char NAME_path[255];
	int num;
    sprintf(NAME_path,"%s%s",NAME,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL == rfp)
	{
        return false;
	}
	fscanf(rfp,"%d",&num);
	char arr[num][20];
	int cnt =-1;
	for(int i=0;i<num;i++)
	{
		fscanf(rfp,"%s",arr[i]);
		if(!strcmp(record,arr[i]))
			cnt = i;
	}
	fclose(rfp);
	if(-1 == cnt)
		return false;
	strcpy(arr[cnt],arr[num-1]);
	num--;
	
	if(num == 0)
	{
		remove(NAME_path);
		return true;
	}
	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s\n",arr[i]);
	}
	fclose(wfp);
	return true;
}

//添加一条记录
bool add_record(char* NAME,char* name,char* record)
{
	char NAME_path[255];
	int num=0;
    sprintf(NAME_path,"%s%s",NAME,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL != rfp)
	{
        fscanf(rfp,"%d",&num);	
	}
	char arr[++num][20];
	if(NULL != rfp)
		for(int i=0;i<num-1;i++)
			fscanf(rfp,"%s",arr[i]);
	strcpy(arr[num-1],record);
	if(NULL != rfp)
		fclose(rfp);

	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s\n",arr[i]);
	}
	fclose(wfp);
	return true;
}
//消除一条书籍信息
bool del_book_all(char* _isbn)
{
	int num=0;
	FILE* rfp = fopen(BOOK_ALL_PATH,"r");
	if(NULL != rfp)
        fscanf(rfp,"%d",&num);	

	char name[num][33];
	char isbn[num][20];
	int cnt =-1;
	for(int i=0;i<num;i++)
	{
		fscanf(rfp,"%s %s",name[i],isbn[i]);
		if(0 == strcmp(isbn[i],_isbn))
			cnt = i;
		printf("%d name:%s isbn:%s %s\n",num,name[i],isbn[i],_isbn);
	}
	fclose(rfp);
	if(-1 == cnt)
	{
		printf("=\n");
		while(1);
		return false;
	}
	strcpy(isbn[cnt],isbn[num-1]);
	strcpy(name[cnt],name[num-1]);
	num--;
	
	if(num == 0)
	{
		remove(BOOK_ALL_PATH);
		return false;
	}
	FILE* wfp = fopen(BOOK_ALL_PATH,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s %s\n",name[i],isbn[i]);
	}
	fclose(wfp);
	return true;
}
//添加一条书籍记录
bool add_book_all(char* _name,char* _isbn)
{
	int num=0;
	FILE* rfp = fopen(BOOK_ALL_PATH,"r");
	if(NULL != rfp)
	{
        fscanf(rfp,"%d",&num);	
	}
	num++;
	char name[num][33];
	char isbn[num][20];
	if(NULL != rfp)
		for(int i=0;i<num-1;i++)
			fscanf(rfp,"%s %s",name[i],isbn[i]);
	strcpy(name[num-1],_name);
	strcpy(isbn[num-1],_isbn);
	if(NULL != rfp)
		fclose(rfp);

	FILE* wfp = fopen(BOOK_ALL_PATH,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s %s\n",name[i],isbn[i]);
	}
	fclose(wfp);
	return true;
}

//删除文件
void del(char* NAME,char* name)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",NAME,name);
	remove(NAME_path);
}


//工号查找操作员
Worker* read_worker_id(char* id)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",NAME_path,id);
    Worker* worker;
    FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
        return NULL;
    fscanf(rfp,"%s %d %s %s %d",
           worker->name,
           &worker->id,
           worker->password,
           worker->phone,
           &worker->sex);
    fclose(rfp);
    return worker;   
}

//写入book_isbn
bool write_book_isbn(Book_TYPE* data)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",BOOK_ISBN_PATH,data->isbn);
 	FILE* wfp = fopen(NAME_path,"w");
    if(NULL == wfp)
        return false;
	fprintf(wfp,"%s %s %s %s %s %lf %u %u %u %u %u %d\n",
          data->name,
          data->isbn,
          data->press,
          data->author,
          data->position,
          data->price,
          data->purchase_num,
          data->now_num,
          data->stock_num,
          data->natural_damage,
          data->person_damage,
          data->off);
	fclose(wfp);
 	return true; 
}

//读出book_isbn
bool read_book_isbn(char* isbn,Book_TYPE* data)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",BOOK_ISBN_PATH,isbn);
 	FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
        return false;
	fscanf(rfp,"%s %s %s %s %s %lf %u %u %u %u %u %d",
          data->name,
          data->isbn,
          data->press,
          data->author,
          data->position,
          &data->price,
          &data->purchase_num,
          &data->now_num,
          &data->stock_num,
          &data->natural_damage,
          &data->person_damage,
          &data->off);
    fclose(rfp);  
	return true; 
}
//book_order  0消除一条预约记录/1查看预约记录
bool del_order(char* isbn,const int mode)
{
	char NAME_path[255]; 
    sprintf(NAME_path,"%s%s",BOOK_ORDER_PATH,isbn);
 	FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
        return NULL;
	size_t num;
	fscanf(rfp,"%d",&num);	
	char arr[num][20];
	for(int i=0;i<num;i++)
			fscanf(rfp,"%s",arr[i]);
	if(look_order_information == mode)
	{
		for(int i=0;i<num;i++)
			printf("第%d位预约的学生是%s\n",i,arr[i]);
	}
	fclose(rfp);
	if(del_order_information == mode)
	{
		num--;
		if(0 == num)
		{
			del(BOOK_ORDER_PATH,isbn);
			return true;
		}		
		FILE* wfp = fopen(NAME_path,"w");
    	if(NULL == rfp)
        	return NULL;		
		fprintf(wfp,"%d\n",num);
		for(int i=1;i<=num;i++)
			fprintf(wfp,"%s",arr[i]);
		fclose(wfp);
		if(!del_record(STUDENT_ORDER_PATH,arr[0],isbn))
			return false;
	}		
	return true;
}

//获取当前时间
Time get_time(void)
{
	struct tm* t0 ;
	Time t1;
	time_t* t = malloc(sizeof(time_t));
	time(t);
	t0 = gmtime(t);
	free(t);
	t1.year = t0->tm_year;
	t1.mon = t0->tm_mon;
	t1.day = t0->tm_mday;
	return t1;
}

//写借书记录
bool write_student_borrow(char* id,char* isbn)
{
	Book_TYPE data;
	read_book_isbn(isbn,&data);

	char NAME_path[255];
	int num=0;
    sprintf(NAME_path,"%s%s",STUDENT_BORROW_PATH,id);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL != rfp)
	{
        fscanf(rfp,"%d",&num);	
	}
	Student_Book arr[++num];
	if(NULL != rfp)
		for(int i=0;i<num-1;i++)
			fscanf(rfp,"%s %s %d %d %d",arr[i].name,arr[i].isbn,&arr[i].borrow.year,&arr[i].borrow.mon,&arr[i].borrow.day);
	strcpy(arr[num-1].name,data.name);
	strcpy(arr[num-1].isbn,isbn);
	arr[num-1].borrow = get_time();
	arr[num-1].back = get_time();
	
	if(NULL != rfp)
		fclose(rfp);

	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
		fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s %s %d %d %d\n",arr[i].name,arr[i].isbn,arr[i].borrow.year,arr[i].borrow.mon,arr[i].borrow.day);
	}
	fclose(wfp);
	if(0 < file_num(BOOK_ORDER_PATH,isbn))
	{
		del_order(isbn,del_order_information);
		del_record(STUDENT_ORDER_PATH,id,isbn);
	}
	else
	{
		data.now_num --;
		data.stock_num --;
		write_book_isbn(&data);	
	}
	return true;
}
//删除借书记录
bool del_borrow_record(char* id,Student_Book* record)
{
	char NAME_path[255];
	int num;
    sprintf(NAME_path,"%s%s",STUDENT_BORROW_PATH,id);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL == rfp)
	{
        return false;
	}
	fscanf(rfp,"%d",&num);
	Student_Book arr[num];
	int cnt=-1;
	for(int i=0;i<num;i++)
	{
		fscanf(rfp,"%s %s %d %d %d",arr[i].name,arr[i].isbn,&arr[i].borrow.year,&arr[i].borrow.mon,&arr[i].borrow.day);
		if(0 == strcmp(record->isbn,arr[i].isbn))
			cnt = i;
	}
	fclose(rfp);
	
	if(-1 == cnt )
		return false;
	
	strcpy(arr[cnt].name,arr[num-1].name);
	strcpy(arr[cnt].isbn,arr[num-1].isbn);
	arr[cnt].borrow = arr[num-1].borrow;
	num--;
	if(num == 0)
	{
		remove(NAME_path);
		return true;
	}
	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s %s %d %d %d\n",arr[i].name,arr[i].isbn,arr[i].borrow.year,arr[i].borrow.mon,arr[i].borrow.day);
	}
	fclose(wfp);
	return true;
}
//写还书记录
bool write_student_back(char* id,Student_Book* data)
{
	char NAME_path[255];
	int num=0;
    sprintf(NAME_path,"%s%s",STUDENT_RETURN_PATH,id);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL != rfp)
	{
        fscanf(rfp,"%d",&num);	
	}
	Student_Book arr[++num];
	if(NULL != rfp) 
		for(int i=0;i<num-1;i++)
			fscanf(rfp,"%s %s %d %d %d %d %d %d",arr[i].name,arr[i].isbn,
							&arr[i].borrow.year,
							&arr[i].borrow.mon,
							&arr[i].borrow.day,
							&arr[i].back.year,
							&arr[i].back.mon,
							&arr[i].back.day);

	strcpy(arr[num-1].name,data->name);
	strcpy(arr[num-1].isbn,data->isbn);
	arr[num-1].borrow = data->borrow;
	arr[num-1].back = get_time();

	if(NULL != rfp)
		fclose(rfp);

	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
		fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
		fprintf(wfp,"%s %s %d %d %d %d %d %d\n",arr[i].name,arr[i].isbn,
							arr[i].borrow.year,
							arr[i].borrow.mon,
							arr[i].borrow.day,
							arr[i].back.year,
							arr[i].back.mon,
							arr[i].back.day);
	
	fclose(wfp);
	return true;
}



//student_return读出学生还书记录
void read_student_return(char* id)
{
	char NAME_path[255];
    	sprintf(NAME_path,"%s%s",STUDENT_RETURN_PATH,id);
 	FILE* rfp = fopen(NAME_path,"r");
    	if(NULL == rfp)
        	return ;
	int num;
	fscanf(rfp,"%d",&num);
	for(int i=0;i<num;i++)
	{
		Student_Book data;
		fscanf(rfp,"%s %s %d %d %d %d %d %d",data.name,data.isbn,
											&data.borrow.year,
											&data.borrow.mon,
											&data.borrow.day,
											&data.back.year,
											&data.back.mon,
											&data.back.day);
		printf("%s %s 借书时间：%d：%d：%d    还书时间：%d：%d：%d\n",data.name,data.isbn,
											data.borrow.year+1900,
											data.borrow.mon+1,
											data.borrow.day,
											data.back.year+1900,
											data.back.mon+1,
											data.back.day);
    }
	fclose(rfp);   
    	fclose(rfp);   
}
	
